//Copyright (c) Microsoft Corporation. All rights reserved.
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Microsoft.Office.OneNote
{
	public class NotebookRoot : Folder
	{
		public NotebookRoot()
		{
		}

		/// <summary>
		/// Clones the Unfiled Notes notebook.
		/// </summary>
		/// <returns>A deep copy of this notebook.</returns>
		public override object Clone()
		{
			throw new Exception("Not Implemented"); // Why would this happen?
		}

		/// <summary>
		/// Useful for debugging.
		/// </summary>
		/// <returns>A string representation of the NotebookRoot.</returns>
		public override string ToString()
		{
			return "Notebooks";
		}

		/// <summary>
		/// Inserts the given node as a child at a specific position.  However in the 
		/// case of a notebook, we ensure that we always end up inserting before any
		/// virtual notebooks.
		/// </summary>
		/// <param name="child">The child node to be added.</param>
		/// <param name="index">The position where this child should be added.</param>
		/// <returns></returns>
		public override ANode InsertChild(ANode child, int index)
		{
			int iLastNotebook;
			for (iLastNotebook = children.Count - 1; iLastNotebook >= 0; iLastNotebook--)
			{
				if (children[iLastNotebook] is Notebook)
					break;
			}

			if (child is Notebook)
				index = Math.Min(index, iLastNotebook + 1);
			else
				index = Math.Max(index, iLastNotebook + 1);

			return base.InsertChild(child, index);
		}

		/// <summary>
		/// Tests to see if this folder can contain the specified child node.
		/// </summary>
		/// <param name="child">The child which we are testing.</param>
		/// <returns>Returns true if the child node is of type folder or section.</returns>
		public override bool CanContain(ANode child)
		{
			if (child is Notebook)
				return true;

			if (child is UnfiledNotes)
			{
				// We better not have more than one Unfiled Notes section:
				for (int iChild = 0; iChild < GetChildCount(); iChild++)
				{
					if (GetChild(iChild) is UnfiledNotes)
					{
						throw new OneNoteException("Attempting to add a second Unfiled Notes notebook.");
					}
				}

				return true;
			}

			return false;
		}

		/// <summary>
		/// Helper method used while serializing to allow every folder sub-class to serialize
		/// it's properties...
		/// </summary>
		/// <param name="serializedNode">Our serialized XmlElement</param>
		protected internal override void SerializeFolderProperties(XmlElement serializedElement)
		{
			// Overrides default property serialization!
		}

		/// <summary>
		/// The serialized Xml element type 
		/// </summary>
		protected override internal string SerializedXmlType
		{
			get { return "Notebooks"; }
		}
	}
}
